package com.cloud.util;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-11-14
 * @version 1.0
 */
public class CommUtil {
	public static final int PATIENT = 1;
	public static final int DOCTOR = 2;
	public static final int CRC = 3;
	public static final String code_prefix = "cloud_invite_code_";
	public static final String code_time_prefix = "cloud_invite_code_time_";

	public static boolean isEmpty(String value) {
		return value == null || value.trim().length() == 0;
	}

	public static String listToString(List<String> list, String separator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i));
			if (i < list.size() - 1) {
				sb.append(separator);
			}
		}
		return sb.toString();
	}
	
	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	public static String getPicturePath(int code) {
		if (Picture.CASE.key == code)
			return Picture.CASE.desc;
		if (Picture.HEAD.key == code)
			return Picture.HEAD.desc;
		if (Picture.LICENSE.key == code)
			return Picture.LICENSE.desc;
		return "";
	}

	/**
	 * MD5加密
	 * 
	 * @param original
	 * @return
	 */
	public static final String MD5(String original) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = original.getBytes();
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(btInput);
			byte[] md = mdInst.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static List<String> getRandomNum(List<String> list, int selected) {
		List<String> reList = new ArrayList<String>();
		Random random = new Random();
		if (list.size() >= selected) {
			for (int i = 0; i < selected; i++) {
				int target = random.nextInt(list.size());
				reList.add(list.get(target));
				list.remove(target);
			}
		} else {
			selected = list.size();
			for (int i = 0; i < selected; i++) {
				int target = random.nextInt(list.size());
				reList.add(list.get(target));
				list.remove(target);
			}
		}
		return reList;
	}
	
	public static String vcode(int length) {
		String[] number = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
		Random rnd = new Random();
		int p = number.length - 1;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			int j = rnd.nextInt(p) + 1;
			sb.append(number[j]);
		}
		return "123456"; //sb.toString();
	}
}
